import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_business_core/business_customer/ful_components/business_customer_details_edit_base_tab_form.dart';

class BusinessCustomerDetailsEditBaseTab extends StatefulWidget {
  BusinessCustomerDetailsEditBaseTab({Key key, @required this.response, @required this.vipId}) : super(key: key);

  final BaseHttpResponseModel response;

  final String vipId;

  @override
  _BusinessCustomerDetailsEditBaseTabState createState() => _BusinessCustomerDetailsEditBaseTabState();
}

class _BusinessCustomerDetailsEditBaseTabState extends State<BusinessCustomerDetailsEditBaseTab> with AutomaticKeepAliveClientMixin implements BaseCatalogResponse {
  Map<String, dynamic> _dataSource;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return BaseScreenRefreshComponent(
      request: new BaseScreenRequestModel(widget.response.inputApi, params: {'vipId': widget.vipId}),
      builder: _buildScaffoldWidget,
    );
  }

  Widget _buildScaffoldWidget(BaseHttpResultModel requestResult) {
    return BaseScreenRefreshComponent(
      request: BaseScreenRequestModel(widget.response.webUrl, method: BaseHttpMethodEnum.GET, params: {'vipid': widget.vipId}),
      builder: (BaseHttpResultModel vipResult) => _buildContent(vipResult, requestResult),
    );
  }

  Widget _buildContent(BaseHttpResultModel vipResult, BaseHttpResultModel requestResult) {
    this._initDataSource(vipResult);
    return ListView(
      padding: EdgeInsets.only(bottom: 20.0),
      children: BaseCatalogController.catalogResponseComponents(requestResult, this.catalog()),
    );
  }

  @override
  Map<String, Widget Function(BaseHttpResponseModel response, dynamic dataSource)> catalog() {
    return {
      'MainBusinessContent': (BaseHttpResponseModel response, dynamic dataSource) => BusinessCustomerDetailsEditBaseTabForm(response: response, dataSource: dataSource, formDataSource: _dataSource),
    };
  }

  /// 初始化数据
  void _initDataSource(BaseHttpResultModel vipResult) {
    dynamic arguments = BaseRouterUtil.getArguments(context);
    String vipId = BaseUtils.getKeyData(arguments, 'vipId').toString();
    Map<String, dynamic> datas = vipResult.data;
    if (datas.containsKey('vipSexName') && datas.containsKey('vipSex')) {
      datas['vipSex'] = {'key': datas['vipSex'], 'value': datas['vipSexName']};
    }
    Map<String, dynamic> dataSource = {'vipId': vipId};
    dataSource.addAll(datas);
    this._dataSource = dataSource;
  }

  @override
  bool get wantKeepAlive => true;
}
